﻿package net.wangit.dev.component;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.dev.LabelRule;
import net.wangit.dev.ReferenceUtil;
import net.wangit.framework.expression.LabelExpression;
import net.wangit.jdbc.DB;
import net.wangit.util.Checker;
import net.wangit.view.tree.Tree;

public class EnumerationTree extends ObjectTree {

	private static final String PREFIX = "_03C";
	private static final String TYPE = Integer.toString(3);
	private static final String ENUMERATION_SQL = "select enumid,enumn,l,dm,dpid,gid from ENUMCONFIG";
	private static final String SQL_ORDER = " order by o,enumid";
	private static final String COUNT_NAME = "rowcount";
	private static final String ITEM_SQL = "select count(*) rowcount from ENUM where enumid=?";

	public EnumerationTree() {
	}

	protected String getItemsTableName() {
		return "ENUM";
	}

	protected String getComponentType() {
		return TYPE;
	}

	protected MapList getObjects(DB db) throws Exception {
		MapList objects = db.query((new StringBuilder(
				EnumerationTree.ENUMERATION_SQL)).append(getAppendLimit())
				.append(EnumerationTree.SQL_ORDER).toString());
		objectCount = Checker.isEmpty(objects) ? 0 : objects.size();
		return objects;
	}

	protected boolean showNullGroup() {
		return true;
	}

	protected String getGroupAttribute() {
		return "oncontextmenu=wmg3()";
	}

	protected int addObject(ActionContext ac, DB db, Tree parent, Row row)
			throws Exception {
		int itemCount = 0;
		Tree object = new Tree();
		String id = row.get("enumid");
		int type = row.getInt("dm", 1);
		object.setId((new StringBuilder(EnumerationTree.PREFIX)).append(id)
				.toString());
		String name = LabelExpression.resolve(ac, row.get("enumn"));
		String status = row.get("l", "0");
		StringBuffer icon = new StringBuffer("/domain/dev/c/");
		icon.append("enum").append(type).append(
				markDataPrivilege(row) ? "1" : "0").append(status).append(
				".png");
		object.setIcon(icon.toString());
		if (check) {
			setEnumeration(object, db, id, name, row);
		} else {
			int referenceCount = ReferenceUtil.getEnumerationReferenceCount(db,
					id);
			itemCount = getItemCount(db, id);
			String label = LabelRule.getLabel(ac, ObjectTreeHelper
					.getLableRule(ac), id, name);
			object.setName((new StringBuilder(String.valueOf(referenceCount)))
					.append(" ").append(label).append(" ").append(itemCount)
					.toString());
			object.setAttribute("oncontextmenu=wm3()");
			object.setAction((new StringBuilder("/dev/enum.do?m=e&eid="))
					.append(id).toString());
		}
		add2Parent(parent, object);
		return itemCount;
	}

	private static int getItemCount(DB db, String id) throws Exception {
		MapList list = db.query(EnumerationTree.ITEM_SQL, id, 2);
		return list.getRow(0).getInt(EnumerationTree.COUNT_NAME, 0);
	}

	protected boolean markDataPrivilege(Row row) {
		return !Checker.isEmpty(row.get("dpid"));
	}

	protected String getAppendLimit() {
		return "";
	}

	protected void setEnumeration(Tree object, DB db, String id, String name,
			Row row) throws Exception {
		object.setName((new StringBuilder(String.valueOf(id))).append(" ")
				.append(name).toString());
	}

	protected void add2Parent(Tree parent, Tree object) {
		parent.add(object);
	}

}
